import React, { useState } from "react";
import axios from "../../utils/index";
import { Form, Input, Button } from "antd-mobile";
import { EyeInvisibleOutline, EyeOutline } from "antd-mobile-icons";
import { useNavigate } from "react-router-dom";
function Index() {
  const naviagte = useNavigate();
  const [visible, setVisible] = useState(false);
  const [form] = Form.useForm();
  const [disabled, setDisabled] = useState(false);
  const onFinish = async (value) => {
    const resp = await axios.post("/register", value);
    console.log(resp);
    
    naviagte("/login")
    
  };

  // const haiolo = () => {
  //   form
  //     .validateFields(["username"])
  //     .then(() => {
  //       setDisabled(false);
  //     })
  //     .catch(() => {
  //       // setDisabled(true);
  //     });
  // };

  return (
    <div>
      <Form
        onFinish={onFinish}
        form={form}
        layout="horizontal"
        footer={
          <Button
            block
            type="submit"
            disabled={disabled}
            color="primary"
            size="large"
          >
            注册
          </Button>
        }
      >
        <Form.Item
          name="username"
          label="账号"
          rules={[
            { required: true, message: "账号不能为空" },
            {
              pattern: /\w+@\w+\.com/,
              message: "请输入正确的邮箱",
            },
          ]}
        >
          <Input onChange={console.log} placeholder="请输入账号" />
        </Form.Item>
        <Form.Item
          label="密码"
          name="password"
          extra={
            <div>
              {!visible ? (
                <EyeInvisibleOutline onClick={() => setVisible(true)} />
              ) : (
                <EyeOutline onClick={() => setVisible(false)} />
              )}
            </div>
          }
        >
          <Input
            placeholder="请输入密码"
            clearable
            type={visible ? "text" : "password"}
          />
        </Form.Item>
      </Form>
    </div>
  );
}

export default Index;
